/**
 * 
 */
package com.atjava.nebula.core.service.impl;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

import com.atjava.nebula.core.dao.BaseDao;
import com.atjava.nebula.core.dao.ibatis.Condition;
import com.atjava.nebula.core.dao.ibatis.projection.IProjection;
import com.atjava.nebula.core.model.BaseModel;
import com.atjava.nebula.core.service.BaseService;
import com.atjava.nebula.core.exception.baseExp.BusinessException;
import com.atjava.nebula.core.exception.baseExp.DataAccessException;
import com.atjava.nebula.core.page.Page;

/**基础Service的实现
 * @author kyo
 *
 */
public class BaseServiceImpl  implements BaseService {
	
	
	private BaseDao baseDao;
	public BaseServiceImpl(BaseDao baseDao) {
		this.baseDao=baseDao;

	}
	protected BaseServiceImpl() {		

	}	

	public BaseDao getBaseDao() {
		return baseDao;
	}

	public void setBaseDao(BaseDao baseDao) {
		this.baseDao = baseDao;
	}

	/* 
	 * @see com.atjava.nebula.core.service.BaseService#ExecSql(java.lang.String)
	 */
	public List<Map<String,Object>> ExecSql(String sql) throws BusinessException {
		
		List<Map<String,Object>> res = null ;
		try {
			res = baseDao.ExecSql(sql);
		} catch (DataAccessException e) {			
			throw new BusinessException(e, "执行SQL语句时出错") ;
		}
		return res ;
	}
	/* 
	 * @see com.atjava.nebula.core.service.BaseService#ExecSql(java.lang.String, java.lang.Object[])
	 */
	public List<Map<String, Object>> ExecSql(String sql, Object... params) throws BusinessException {
		List<Map<String,Object>> res = null ;
		try {
			res = baseDao.ExecSql(sql,params);
		} catch (DataAccessException e) {			
			throw new BusinessException(e, "执行SQL语句时出错") ;
		}
		return res ;
	}


	/* 
	 * @see com.atjava.nebula.core.service.BaseService#getAll(java.lang.Class)
	 */
	public <T extends BaseModel> List<T> getAll(Class<T> entityClass)
			throws BusinessException {
		
		List<T> res = null; 
		try {
			res = baseDao.getAll(entityClass);
		} catch (DataAccessException e) {
			throw new BusinessException(e, "获取所有对象时出错") ;
		}
		return res ;
	}




	/* 
	 * @see com.atjava.nebula.core.service.BaseService#list(com.atjava.nebula.core.model.BaseModel)
	 */
	public <T extends BaseModel> List<T> list(T obj) throws BusinessException {
		
		List<T> res = null ;
		try {
			res = baseDao.list(obj);
		} catch (DataAccessException e) {
			throw new BusinessException(e, "获取对象时出错") ;
		}
		return res ;
	}



	/* 
	 * @see com.atjava.nebula.core.service.BaseService#list(java.lang.Class, com.atjava.nebula.core.dao.ibatis.Condition)
	 */
	public <T extends BaseModel> List<T> list(Class<T> entityClass,
			Condition cond) throws BusinessException {
		
		List<T> res = null ;
		try {
			res = baseDao.list(entityClass, cond);
		} catch (DataAccessException e) {
			throw new BusinessException(e, "获取对象时出错") ;
		}
		return res ;
		
	}
	/* 
	 * @see com.atjava.nebula.core.service.BaseService#list(java.lang.Class, com.atjava.nebula.core.dao.ibatis.projection1.IProjection, com.atjava.nebula.core.dao.ibatis.Condition)
	 */
	public <T extends BaseModel> List<Map<String, Object>> list(Class<T> entityClass, IProjection projection, Condition cond) throws BusinessException {
		List<Map<String, Object>> res = null ;
		try {
			res = baseDao.list(entityClass, projection,cond);
		} catch (DataAccessException e) {
			throw new BusinessException(e, "获取对象时出错") ;
		}
		return res ;
	}
	/* 
	 * @see com.atjava.nebula.core.service.BaseService#list(com.atjava.nebula.core.model.BaseModel, com.atjava.nebula.core.dao.ibatis.projection1.IProjection)
	 */
	public <T extends BaseModel> List<Map<String, Object>> list(T obj, IProjection projection) throws BusinessException {
		List<Map<String, Object>> res = null ;
		try {
			res = baseDao.list(obj, projection);
		} catch (DataAccessException e) {
			throw new BusinessException(e, "获取对象时出错") ;
		}
		return res ;
	}

	/* 
	 * @see com.atjava.nebula.core.service.BaseService#page(com.atjava.nebula.core.model.BaseModel, int, int)
	 */
	public <T extends BaseModel> Page<T> page(T obj, int pageNo, int pageSize)
			throws BusinessException {
		
		Page<T> page = null ;
		try {
			page = baseDao.page(obj, pageNo, pageSize);
		} catch (DataAccessException e) {
			throw new BusinessException(e, "分页获取对象时出错") ;
		}
		return page ;
	}


	/* 
	 * @see com.atjava.nebula.core.service.BaseService#page(java.lang.Class, com.atjava.nebula.core.dao.ibatis.Condition, int, int)
	 */
	public <T extends BaseModel> Page<T> page(Class<T> entityClass,
			Condition cond, int pageNo, int pageSize) throws BusinessException {
		
		Page<T> page = null ;
		try {
			page = baseDao.page(entityClass, cond, pageNo, pageSize);
		} catch (DataAccessException e) {
			throw new BusinessException(e, "分页获取对象时出错") ;
		}
		return page ;
	}
	/* 
	 * @see com.atjava.nebula.core.service.BaseService#page(java.lang.Class, com.atjava.nebula.core.dao.ibatis.projection1.IProjection, com.atjava.nebula.core.dao.ibatis.Condition, int, int)
	 */
	public <T extends BaseModel> Page<Map<String, Object>> page(Class<T> entityClass, IProjection projection, Condition cond, int pageNo, int pageSize) throws BusinessException {
		Page<Map<String, Object>> page = null ;
		try {
			page = baseDao.page(entityClass, projection,cond, pageNo, pageSize);
		} catch (DataAccessException e) {
			throw new BusinessException(e, "分页获取对象时出错") ;
		}
		return page ;
	}
	/* 
	 * @see com.atjava.nebula.core.service.BaseService#page(com.atjava.nebula.core.model.BaseModel, com.atjava.nebula.core.dao.ibatis.projection1.IProjection, int, int)
	 */
	public <T extends BaseModel> Page<Map<String, Object>> page(T obj, IProjection projection, int pageNo, int pageSize) throws BusinessException {
		Page<Map<String, Object>> page = null ;
		try {
			page = baseDao.page(obj, projection, pageNo, pageSize);
		} catch (DataAccessException e) {
			throw new BusinessException(e, "分页获取对象时出错") ;
		}
		return page ;
	}	

	/* 
	 * @see com.atjava.nebula.core.service.BaseService#remove(com.atjava.nebula.core.model.BaseModel)
	 */
	public <T extends BaseModel> void remove(T obj) throws BusinessException {
		
		try {
			baseDao.remove( obj);
		} catch (DataAccessException e) {
			throw new BusinessException(e, "删除一个对象时出错") ;
		}
	}


	/* 
	 * @see com.atjava.nebula.core.service.BaseService#updateById(com.atjava.nebula.core.model.BaseModel)
	 */
	public <T extends BaseModel> void updateById(T obj) throws BusinessException {
		
		try {
			baseDao.updateById(obj);
		} catch (DataAccessException e) {
			throw new BusinessException(e, "修改一个对象时出错") ;
		}
	}
	
	/* 
	 * @see com.atjava.nebula.core.service.BaseService#updateAllById(com.atjava.nebula.core.model.BaseModel)
	 */
	public <T extends BaseModel> void updateAllById(T obj) throws BusinessException {
		try {
			baseDao.updateAllById(obj);
		} catch (DataAccessException e) {
			throw new BusinessException(e, "修改一个对象时出错") ;
		}		
	}
	/* 
	 * @see com.atjava.nebula.core.service.BaseService#getById(java.lang.Class, java.io.Serializable)
	 */
	public <T extends BaseModel, ID extends Serializable> T getById(
			Class<T> entityClass, ID id) throws BusinessException {
		
		T obj = null ;
		try {
			obj = baseDao.getById(entityClass, id);
		} catch (DataAccessException e) {
			throw new BusinessException(e, "根据ID获取一个对象时出错") ;
		}
		return obj ;	
	}
	/* 
	 * @see com.atjava.nebula.core.service.BaseService#removeById(java.lang.Class, java.io.Serializable)
	 */
	public <T extends BaseModel, ID extends Serializable> void removeById(
			Class<T> entityClass, ID id) throws BusinessException {
		
		try {
			baseDao.removeById(entityClass, id);
		} catch (DataAccessException e) {
			throw new BusinessException(e, "根据ID删除一个对象时出错") ;
		}		
	}

	

	/* 
	 * @see com.atjava.nebula.core.service.BaseService#count(java.lang.Class)
	 */
	public <T extends BaseModel> Integer count(Class<T> entityClass) throws BusinessException {
		Integer c=null;
		try {
			c=baseDao.count(entityClass);
		} catch (DataAccessException e) {
			throw new BusinessException(e, "统计记录条数时出错") ;
		}		
		return c;
	}
	/* 
	 * @see com.atjava.nebula.core.service.BaseService#count(java.lang.Class, com.atjava.nebula.core.dao.ibatis.Condition)
	 */
	public <T extends BaseModel> Integer count(Class<T> entityClass, Condition cond) throws BusinessException {
		Integer c=null;
		try {
			c=baseDao.count(entityClass,cond);
		} catch (DataAccessException e) {
			throw new BusinessException(e, "统计记录条数时出错") ;
		}		
		return c;
	}
	/* 
	 * @see com.atjava.nebula.core.service.BaseService#count(com.atjava.nebula.core.model.BaseModel)
	 */
	public <T extends BaseModel> Integer count(T obj) throws BusinessException {
		Integer c=null;
		try {
			c=baseDao.count(obj);
		} catch (DataAccessException e) {
			throw new BusinessException(e, "统计记录条数时出错") ;
		}		
		return c;
	}
	
	/* 
	 * @see com.atjava.nebula.core.service.BaseService#count(java.lang.Class, com.atjava.nebula.core.dao.ibatis.projection.IProjection, com.atjava.nebula.core.dao.ibatis.Condition)
	 */
	public <T extends BaseModel> Integer count(Class<T> entityClass, IProjection projection, Condition cond) throws BusinessException {
		Integer c=null;
		try {
			c=baseDao.count(entityClass,projection,cond);
		} catch (DataAccessException e) {
			throw new BusinessException(e, "统计记录条数时出错") ;
		}		
		return c;
	}
	/* 
	 * @see com.atjava.nebula.core.service.BaseService#count(com.atjava.nebula.core.model.BaseModel, com.atjava.nebula.core.dao.ibatis.projection.IProjection)
	 */
	public <T extends BaseModel> Integer count(T obj, IProjection projection) throws BusinessException {
		Integer c=null;
		try {
			c=baseDao.count(obj,projection);
		} catch (DataAccessException e) {
			throw new BusinessException(e, "统计记录条数时出错") ;
		}		
		return c;
	}
	
	/* 
	 * @see com.atjava.nebula.core.service.BaseService#create(com.atjava.nebula.core.model.BaseModel)
	 */
	public <T extends BaseModel, ID extends Serializable> ID create(T obj) throws BusinessException {
		ID o = null ;
		try {
			o = baseDao.create(obj) ;
		} catch (DataAccessException e) {
			throw new BusinessException(e, "创建一个对象时出错") ;
		}
		return o ;
	}
	

}
